class Transaction < ActiveRecord::Base
     has_one :user
     has_one :stock

     #I looked in the view for transactions and saw that the input to decide
     # whether the transaction type was a buy or sell was input into a textbox.
     # Correct me if I am wrong on that. If it is a textbox entry then I thought it
     # would be a good idea to validate that the person is actually entering the
     # words Buy or Sell and not some mumbo jumbo instead. The same goes for some of 
     # the other fields. I was kind of confused when I looked at the view for the input.
     # Does some of this information line stock ID populate via a dropdown selection
     # or is the user supposed to remember stock ID's by themself? 


     #some of this validation is going off the idea that input is provided by the user via textboxes. This is the assumption.
     validates :money, :prescence => true
     validates :quantity :prescence => true
     validates :stock_id :prescence => true
     validates :transtype :prescence => true

     validates_numericality_of :money, :allow_nil => false,
     :greater_then_or_equal_to => 0, :message => "The value of money must be greather than or equal to zero in a transaction."

     validates_numericality_of :quantity, :allow_nil => false,
     :greather_then => 0, :message => "Your transaction quantity must be greather than zero."

     validates_format_of :transtype, :with => /(B|b)uy | (S|s)ell/, :message => "Password must be a mix of letters and numbers."

     #checking that the stock used is a valid stock from the stock table.
     validate :check_foreign_stock_id
     def check_foreign_stock_id
          if((transaction == Stock.find_by(stock_id)) == nil)
               errors.add(:stock_id, "Invalid stock id entered. Please try again.")
          end
     end

end
